$(function() {
    //初始化
    Begin();
})
var isAdd = false;//是否为新增标记
var s_searchValue="";
var s_productCode="";
var s_licenseId = "";

var dialogInfos = new Vue({
	el: "#dialogInfos",
	data: function() {
		var customer = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入客户名称'));
			}
			return callback();
		};
		var startDate = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入开始时间'));
			}
			return callback();
		};
		var endDate = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入到期时间'));
			}
			return callback();
		};
		var serialNo = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入注册码'));
			}
			if(value.length!=36){
				return callback(new Error('请输入正确的注册码!'));
			}
			return callback();
		};
		var license = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请生成licence'));
			}
			var isLicense = false;
			var self = dialogInfos;
			var params = self.customForm;
			VanyAjax("/system/licenseManager/checkLicense", params).done(function(data) {
				var isOk = data["isOk"];
				if(isOk=="OK"){
					return callback();
				}else{
					return callback(new Error('请生产正确的license信息！'));
				}
			}, true, self);
			
		};
		return {
			pickerOptions1:{
				shortcuts: [{
	            text: '今天',
	            onClick(picker) {
	              picker.$emit('pick', new Date());
	            }
	          }, {
	            text: '昨天',
	            onClick(picker) {
	              const date = new Date();
	              date.setTime(date.getTime() - 3600 * 1000 * 24);
	              picker.$emit('pick', date);
	            }
	          }, {
	            text: '一周前',
	            onClick(picker) {
	              const date = new Date();
	              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
	              picker.$emit('pick', date);
	            }
	          }]
			},
			customForm: {
				customer: '',
				startDate:'',
				endDate:'',
				serialNo:'',
				license:''
			},
			rules: {
				customer: [{
					required: true,
					validator: customer,
					trigger: 'blur'
				}],
				startDate: [{
					required: true,
					validator: startDate,
					trigger: 'blur'
				}],
				endDate: [{
					required: true,
					validator: endDate,
					trigger: 'blur'
				}],
				serialNo: [{
					required: true,
					validator: serialNo,
					trigger: 'blur'
				}],
				license: [{
					required: true,
					validator: license,
					trigger: 'blur'
				}]
			},
			dialogVisible:false
		}
	},
	methods: {
		submitForm: function(formName) {
			var self = this;
			var params = self.customForm;
			this.$refs["customForm"].validate(function(valid) {
				if(!valid) {
					return false;
				} else {
					params["productCode"] = s_productCode;
					params["licenseId"] = s_licenseId;
					params["startDate"] = dateToString(params["startDate"]);
					params["endDate"] = dateToString(params["endDate"]);
					var url = "";
					if(isAdd){
						url = "/system/licenseManager/add";
					}else{
						url = "/system/licenseManager/update"
					}
					VanyAjax(url, params).done(function(data) {
						this.dialogVisible = false;
						table.getData({
						    searchValue: s_searchValue,
						    productCode: s_productCode
						}, true);
					}, true, self);
				}
			})
		},
		resetForm: function(formName) {
			dialogInfos.customForm.customer = '';
        	dialogInfos.customForm.startDate = '';
        	dialogInfos.customForm.endDate = '';
        	dialogInfos.customForm.serialNo = '';
        	dialogInfos.customForm.license = '';
		}, //重置
		cancelForm: function(formName) {
			this.dialogVisible = false;
			isAdd = false;
		},
		getLicence: function(formName) {
			var self = this;
			var params = self.customForm;
			var isPass = true;
			if(isPass){
				VanyAjax("/system/licenseManager/createLicense", params).done(function(data) {
					var licenseStr = data["license"];
					this.customForm.license = licenseStr;
				}, true, self);
			}else{
				this.$message('请正确填写表单上除了license的其余所有信息！');
			}
		},
		handleClose:function(){
			debugger;
			this.dialogVisible = false;
		}
	}
});


var vanytree = new VanyTree({
    el: "treeLeft",
    url: "/system/licenseManager/queryProducts",
    type: "POST",
    title: "产品列表",
    neeMove:true, //是否仅在左侧展示
    defaultProps: {
    	"children": 'children',
        "label": 'productName',
        "id": "productCode"
    },
    //节点点击事件
    nodeClick: function(data, node, nodeComponent) {
    	s_productCode=data.productCode;
    	table.getData({
    	    searchValue: "",
    	    productCode: data.productCode
    	}, true);
    }
});
vanytree.getData();

var table = new VanyTable({
    el: "#tableR",
    url: "/system/licenseManager/queryLicense",
    methodTypes:"POST",  //数据获取方式
    index: true, //选框
    selection: false, //编号
    pages: true, //是否需要分页，与scrollPage最好二选一
    pageSize: 10,
    pageSizes: [10,20,30],
    scrollPage: false, //是否滚动分页，与pages最好二选一
    //height: 200,  //固定高度，滚动分页时需要使用
    // right、topRight、bottomLeft
    btnPostion: "topRight", //底部菜单所在位置
    btns: [{
        authority: "base_user_add",
        name: "添加客户",
        click: function() {
        	if(s_productCode==""){
        		this.$message('需要先选择左边树的产品才能增加用户注册信息！');
        	}else{
        		isAdd = true;
        		s_licenseId = "";
            	dialogInfos.dialogVisible = true;
            	dialogInfos.customForm.customer = '';
            	dialogInfos.customForm.startDate = '';
            	dialogInfos.customForm.endDate = '';
            	dialogInfos.customForm.serialNo = '';
            	dialogInfos.customForm.license = '';
        	}
        }
    }], //底部菜单
    keys: [{
        "key": "customer",
        "name": "客户名称"
    }, {
        "key": "serialNo",
        "name": "安装序列号"
    }, {
        "key": "startDate",
        "name": "开始日期"
    }, {
        "key": "endDate",
        "name": "结束日期"
    }, {
        "key": "status",
        "name": "状态",
        "template": ''
    }, {
        "key": "operation",
        "name": "操作",
        "template": '<span @click="handleEdit(scope.$index, scope.row)">编辑</span> | <span @click="handleDel(scope.$index, scope.row)">删除</span>'
    }] //表格title
});

table.getData({
    searchValue: "",
    productCode: ""
}, true);

var msg = [];
//search
table.on("search", function(index, row) {
	table.getData({
	    searchValue: table.searchValue,
	    productCode: ""
	}, true);
});
table.on("handleEdit", function(index, row) {
	isAdd = false;
	msg = row;
	dialogInfos.dialogVisible = true;
	dialogInfos.customForm.customer = row.customer;
	dialogInfos.customForm.startDate = row.startDate;
	dialogInfos.customForm.endDate = row.endDate;
	dialogInfos.customForm.serialNo = row.serialNo;
	dialogInfos.customForm.license = row.license;
	s_licenseId = row.licenseId;
});
table.on("handleDel", function(index, row) {
	msg = row;
	this.$confirm('确认删除该所选客户注册信息？', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(function() {
		VanyAjax("/system/licenseManager/delete", 
		{
			licenseId: msg.licenseId
		}).done(function(data) {
			table.getData({
			    searchValue: s_searchValue,
			    productCode: s_productCode
			}, true);
		}, false, this);
	}).catch(function() {})
});



//初始化
function Begin() {
    //面包屑
    new Vue().$mount('#crumbs');
    sessionStorage.Jurisdiction = "base_tag_addBaseRootTag,base_team_delTeamUsesr,base_user_update,base_tag_modifyBaseTag,base_team_modify,base_role_modifyRFAAndRemove,base_tagUser_export,base_customerUser_export,base_role_modifyRoleInfo,base_customer_import,base_org_delete,base_customer_addTag,base_team_delete,base_org_export,base_role_modifyRoleUser,base_user_resetUserStatus,base_org_import,base_user_export,base_tag_modifyTag,base_customer_add,base_customer_modifyTag,base_user_resetUserPassword,base_user_import,base_team_addTeamUsers,base_tag_deleteTag,base_user_deleteUsers,base_org_update,base_role_deleteRoleUser,base_user_moveUsers,base_teamUser_export,base_tag_addBaseTag,base_team_add,base_tag_selectTagUser,base_tag_addTag,base_customer_delCustomer,base_customer_addRootTag,base_tag_addRootTag,base_org_add,base_user_updateOrderNum,base_tag_deleteTagUser,base_tag_deleteBaseTag,base_customer_deleteTag,base_org_updateOrderNum,base_user_add"
}
